package com.jerry.base.io.file;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA
 * User: Jerry
 * Date: 2024/1/23
 * Time: 23:09
 * Description:
 */
@Slf4j
public class FileCopy {

    /**
     * 以字节为单位的拷贝
     *
     * @throws IOException
     */
    @Test
    public void copy() throws IOException {
        String filePath = "src/main/resources/io/girl.jpg";
        File file = new File(filePath);

        String copyPath = "src/main/resources/io/girl_copy.jpg";
        File copy = new File(copyPath);

        try (FileInputStream fileInputStream = new FileInputStream(file);
             FileOutputStream fileOutputStream = new FileOutputStream(copy)) {
            byte[] bytes = new byte[1024];
            int length;
            while ((length = fileInputStream.read(bytes)) != -1) {
                // 一边读取一边拷贝
                fileOutputStream.write(bytes, 0, length);
            }
        }
        log.info("拷贝完毕！");
    }

}
